import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        const temp = (context as any).args[0]
        const { url, method, params, query, body } = temp as any;
        const _params = Object.keys(params).length ? JSON.stringify(params) : ''
        const _body = Object.keys(query).length || Object.keys(body).length ? JSON.stringify(method === 'GET' ? query : body) : ''
        const str = `${method} :>> ${url} ${_params} ${_body}`
        return next
            .handle()
            .pipe(
                tap(() => console.log(str)),
            );
    }
}